package commander;

import fileoperation.FileRead;
import fileoperation.FileReader;
import repository.Repository;

import java.io.File;
import java.util.ArrayList;

/*
1、从当前的HEAD指针指向的commit开始，依次向前遍历所有的commit信息
2、日志中需要包含当前commit的hash值，作者信息，commit时间戳，以及commit信息。调用FileReader函数
3、打印日志时不需要考虑其他分支的情况。
4、FileReader读取顺序为tree、parent、author、commit、message
 */
public class log extends Command{

    File currentBranch = new File(Repository.getGitDir() + File.separator + FileRead.readBranchFromHead(this.head));
    //当前Head所指分支
    String branchname = currentBranch.getName();
    String currentCommit = FileRead.readCommitFromBranch(currentBranch);
    //Branch首元节点
    String parent = FileRead.readParentFromCommit(new File(wrapObjects(currentCommit)));
    //Commit父节点
    String res = " ";
    //Res为被打印信息
    ArrayList<String> commitList =  new ArrayList<>();

    public void printlog(){
        System.out.println("Current branch is " + branchname + ":");
        try{
            System.out.println(FileReader.deserializeFileValue(currentBranch));
            commitList = FileReader.deserializeFileToList(currentBranch);
            if(commitList.size() != 0){
                for(int i=0 ; i<commitList.size() ; i++){
                    res += "\n";
                    res += commitList.get(i);
                }
            }else{
                res += "Current branch " + branchname + " is empty";
                System.out.println(res);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

   /* public static void main(String[] args){
        new log().printlog();
    }*/
}